package com.lanou3g.k12.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.CourseType;
import com.lanou3g.k12.service.CourseTypeService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import com.lanou3g.k12.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class CourseTypeController {

    @Autowired
    private CourseTypeService courseTypeService;


    /**
     * 查询，反到前台
     * @param page 第几页
     * @param keyword  关键字
     * @param size  每页显示数量
     * @return
     */
    @GetMapping("/course/types")
    public K12Result findAll(@RequestParam(value = "page",defaultValue = "1")Integer page,
                             String keyword,
                             @RequestParam(value = "size",defaultValue = "30") Integer size) {
        try {
            //分页
            Page<Object> p = PageHelper.startPage(page, size);
            if (keyword != null && !keyword.trim().equals("")) {
                //查关键字
                courseTypeService.search(keyword);
            } else {
                //查询所有
                courseTypeService.findAll();
            }
            return K12ResultUtils.ok(p);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    /**
     * 新增
     * @param courseType
     * @return
     */
    @PostMapping("/course/type")
    public K12Result add(CourseType courseType) {
        try{
            courseTypeService.add(courseType);
            return K12ResultUtils.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }


    /**
     * 通过ID查找课程类型
     * @param id 课程id
     * @return
     */
    @GetMapping("/course/type/{id}")
    public K12Result selectTypeById(@PathVariable Long id) {
        try {
            CourseType courseType = courseTypeService.selectTypeById(id);
            return K12ResultUtils.ok(courseType);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    /**
     * 通过ID删除课程类型
     * @param id 课程id
     * @return
     */
    @DeleteMapping("/course/type/{id}")
    public K12Result deleteTypeById(@PathVariable Long id) {
        try{
            courseTypeService.deleteTypeById(id);
            return K12ResultUtils.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    /**
     * 通过id删除多个
     * @param ids 课程id
     * @return
     */
    @DeleteMapping("/course/types/{ids}")
    public K12Result deleteTypeByIds(@PathVariable Long[] ids) {
        try {
            courseTypeService.deleteTypeByIds(ids);
            return K12ResultUtils.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    /**
     * 通过id修改
     * @param id 课程id
     * @param courseType
     * @return
     */
    @PutMapping("/course/type/{id}")
    public K12Result updateTypeById(@PathVariable Long id,CourseType courseType){

        try{
            courseTypeService.updateTypeById(id,courseType);
            return K12ResultUtils.ok("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

}
